home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / os2 / racer13.zip / SRC / RACER.C next >
Text File  |  1996-09-26  |  5KB  |  175 lines

  1. /*
  2.    Title  : Racer, Racing car game version 1.3
  3.    Author : Antonino Iannella, September 25th, 1996
  4.    Description:
  5.           A simple command-line car being driven down a
  6.           fast, winding track.   The white lines are the
  7.           boundaries in which to keep the '╬' car.
  8.  
  9.           It is compilable under GCC (with no 
  10.           optimisations) using
  11.  
  12.           gcc -Zomf -Zsys -o racer.exe racer.c racer.def
  13.  
  14.           If using a different compiler, the only command
  15.           which may need changing is the _read_kbd() in
  16.           function main().  It just gets any character
  17.           without waiting for user input, and puts it in
  18.           'let'.  This is not the best way to read keyboard
  19.           input, so if anyone knows a better method, I'd
  20.           like to know...
  21.  
  22. Revision:
  23.           Added a delay period with every write to slow down
  24.           the game. Uses _sleep2(), an EMX-specific command.
  25.           Also reports the distance travelled; every line is
  26.           LINE_DISTANCE kilometres.
  27.           Added a prompt to race again after a crash.
  28.           Added the RACER.DEF file/
  29. */
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <sys/kbdscan.h>
  34.  
  35.                           /* Defines the screen width (SCR_WIDTH characters) */
  36. #define  SCR_WIDTH         80
  37.                             /* Track width; the space between the boundaries */
  38. #define  TRACK_WIDTH       20
  39.  
  40. /* Revised 25-9-96 */
  41. #define  LINE_DISTANCE     0.01 
  42. #define  SLOW_THE_PC_DOWN  10                     /* Milliseconds per delay */
  43.  
  44. int cpos,                       /* Car position from left side of the screen */
  45.     lpost,
  46.     rpost;                             /* Set left and right road boundaries */
  47.  
  48. unsigned long rnd;                                /* Random number generator */
  49.  
  50. double Travelled;                                  /* For distance travelled */
  51.  
  52. /* ------------------------ GRUNDIG - Made for you! ------------------------ */
  53.  
  54. void Oneline(void)
  55. /* Draws the next line, representing the track
  56.    and car, at the bottom of the screen.
  57. */
  58. {
  59.   int pt1,
  60.       pt2,
  61.       pt3;                                                       /* Counters */
  62.  
  63.   rpost=lpost+TRACK_WIDTH;
  64.  
  65.   for(pt1=0; pt1 < lpost; pt1++)                           /* Draw left edge */
  66.    printf(" ");
  67.  
  68.   printf("█");
  69.  
  70.   for(pt2=lpost+1; pt2 < cpos; pt2++)                        /* Draw the car */
  71.    printf(" ");
  72.  
  73.   printf("╬");
  74.  
  75.   for(pt3=cpos+1; pt3 < rpost; pt3++)                     /* Draw right edge */
  76.    printf(" ");
  77.  
  78.   printf("█\n");
  79.  
  80.   /* Delay the process to make the game run at a more human speed.
  81.      EMX-only command. */
  82.   _sleep2(SLOW_THE_PC_DOWN);
  83.  
  84.   Travelled += LINE_DISTANCE;                 /* Increase distance travelled */
  85. }
  86.  
  87. /* ------------------------ GRUNDIG - Made for you! ------------------------ */
  88.  
  89. void Crash()
  90. /* Exits the game if 'q' was pressed, or when
  91.    the car 'crashes' into a boundary.
  92. */
  93. {
  94.   char goagain;
  95.  
  96.   printf("\nSmack! Prang! You lasted %.3f km (%.4f miles).\n", Travelled, Travelled/1.6);
  97.  
  98.   fflush(stdin);
  99.   printf("Would you like to drive again [Y/N]");
  100.   goagain=getchar();
  101.  
  102.   if ((goagain == 'N') || (goagain == 'n'))
  103.   {
  104.     printf("\n\nThank you for trying Racer. ");
  105.     printf("Tell me what you think - antonino@usa.net.\n");
  106.     exit(0);
  107.   }
  108.   else                                       /* Reset car to starting position */
  109.   {
  110.     Travelled=0;
  111.     lpost=25;                                        /* Set left road boundary */
  112.     cpos=lpost+TRACK_WIDTH/2;                       /* Initialise car position */
  113.   }
  114. }
  115.  
  116. /* ------------------------ GRUNDIG - Made for you! ------------------------ */
  117.  
  118. void track(int kkey)
  119. /* Moves the car left or right, and uses random
  120.    numbers to decide which way to move the track.
  121. */
  122. {
  123.   switch (kkey)
  124.   {
  125.     case 75 : cpos--;                                          /* Move left  */
  126.               Oneline();
  127.               break;
  128.  
  129.     case 77 : cpos++;                                          /* Move right */
  130.               Oneline();
  131.               break;
  132.  
  133.     default : Oneline();
  134.  
  135.   }
  136.  
  137. /* RAND_MAX defined in <stdlib.h>, is the maximum random number.
  138.    This part says that if the random number is less than 1 3rd of RAND_MAX,
  139.    move left.  If between 1 and 2 3rds, don't move.  Else, move right. */
  140.  
  141.   if ((cpos==lpost) || (cpos==lpost+TRACK_WIDTH))
  142.     Crash();
  143.  
  144.   rnd=rand();
  145.  
  146.   if ((rnd< (RAND_MAX/3)) && (lpost>0))
  147.      lpost--;
  148.   else
  149.     if ((rnd> (2*(RAND_MAX/3))) && (rpost < SCR_WIDTH))
  150.      lpost++;
  151. }
  152.  
  153. /* ------------------------ GRUNDIG - Made for you! ------------------------ */
  154.  
  155. main()
  156. {
  157.   char let;
  158.  
  159.   lpost=25;                                        /* Set left road boundary */
  160.   cpos=lpost+TRACK_WIDTH/2;                       /* Initialise car position */
  161.   Travelled=0;
  162.  
  163.   printf("\nRacer version 1.3 by Antonino Iannella, September 1996.\n");
  164.  
  165.   while(1)
  166.   {
  167.     let=_read_kbd(0, 0, 0);        /* No echoing, no waiting, disable CTRL-C */
  168.     fflush(stdin);
  169.  
  170.     if ((let=='Q') || (let=='q'))
  171.       Crash();
  172.     else
  173.       track(let);                                        /* Print next track */
  174.   }
  175. }